package com.wss.module.wan.ui.project.mvp;

import com.wss.common.base.mvp.BasePresenter;
import com.wss.common.utils.JsonUtils;
import com.wss.common.utils.ValidUtils;
import com.wss.module.wan.bean.Article;
import com.wss.module.wan.bean.Classification;
import com.wss.module.wan.ui.project.mvp.contract.ProjectContract;
import com.wss.module.wan.ui.project.mvp.model.ProjectModel;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

/**
 * Describe：项目控制器
 * Created by 吴天强 on 2018/11/15.
 */
public class ProjectPresenter extends BasePresenter<ProjectModel, ProjectContract.View>
        implements ProjectContract.Presenter {

    @Override
    protected ProjectModel createModule() {
        return new ProjectModel(getLifecycleOwner());
    }

    @Override
    public void start() {
        getProjectType();
    }

    /**
     * 获取项目分类
     */
    private void getProjectType() {
        getModel().getProjectType(new SimpleCallBack<List<Classification>>() {
            @Override
            public void onError(ApiException e) {
            }

            @Override
            public void onSuccess(List<Classification> classifications) {

                getView().refreshProjectTypeList(classifications);
            }
        });
    }

    @Override
    public void getProject() {
        showLoading();
        getModel().getProject(getView().getPage(), getView().getTypeId(), new SimpleCallBack<String>() {
            @Override
            public void onError(ApiException e) {
                getView().onError("", e.getMessage());
            }

            @Override
            public void onSuccess(String s) {
                dismissLoading();
//                List<Article> articleList = JsonUtils.getList(JsonUtils.getString(s, "datas"), Article.class);
                String data = JsonUtils.getString(s, "data");
                List<Article> articleList = JsonUtils.getList(JsonUtils.getString(data, "datas"), Article.class);
                if (ValidUtils.isValid(articleList)) {
                    getView().refreshProjectList(articleList);
                } else {
                    getView().onEmpty("");
                }
            }
        });
    }
}